<?php

namespace App\Console\Commands;

use App\Http\Model\CrossRoomPkRecord;
use App\Http\Model\Union;
use App\Http\Services\RoomServices;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CrossRoomPkAnchorMission extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CrossRoomPkAnchorMission';

    protected $crossRoomPkModel;
    protected $room = 'room';

    protected $roomService;

    protected $crossRoomPkAnchorPkMission = 'CROSS:ROOM:PK:MISSION:ANCHORMIC';//HASH 主播上麦每日PK缓存 CROSS:ROOM:PK:MISSION:ANCHORMIC {$userId}=>time();
    protected $crossRoomPkMissionComplate = 'CROSS:ROOM:PK:MISSION:ANCHORMIC:COMPLATE:';//主播完成任务缓存当天

    protected $log = 'cross_room_pk_anchor_mission';//日志
    protected $error_log = 'error_cross_room_pk_anchor_mission';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '主播上PK麦每日任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->crossRoomPkModel = new CrossRoomPkRecord();
        $this->roomService = new RoomServices();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //获取所有上麦的用户
        $allData = Redis::HGETALL($this->crossRoomPkAnchorPkMission);
        do{
            //查看跨房PK中的房间
            $crossRoomList = DB::table($this->room)->where('room_mode',6)->get(['room_id']);
            $crossRoomList = objToArray($crossRoomList);
            if(!empty($crossRoomList)){
                foreach($crossRoomList as $r){
                    $micList = $this->roomService->microUserList(['room_id'=>$r['room_id']]);
                    if($micList['code'] == 0){
                        //主播id
                        $hostUserId = $micList['data']['host_micro']['user_id'];
                        $this->finishJob($hostUserId);
                        $anchorList = array_column($micList['data']['general_micro_list'],'user_id');
                        foreach($anchorList as $user){
                            $this->finishJob($user);
                        }
                    }
                }
            }
            if(empty($allData)){
                write_log($this->log,'INFO 无数据',$allData);
                break;
            }
            //处理数据
            $now = Carbon::now()->toDateTimeString();
            
            foreach($allData as $k=>$v){
                //比对当前时间和上麦时间是否大于1分钟
                $diffTime = Carbon::parse($now)->diffInMinutes(date('Y-m-d H:i:s',$v));
                if($diffTime >= 1 ){
                    //每日任务更新
                    $this->finishJob($k);
                } else {
                    write_log($this->log,'INFO 未满足上麦时长',['user_id'=>$k,'diffTime'=>$diffTime]);
                }
            }
        }while(false);
    }

    public function finishJob($userId){
        if(!empty(Redis::get($this->crossRoomPkMissionComplate.$userId))){
            write_log($this->log,'INFO 当天已完成',$userId);
            return false;
        }
        $missList = $this->crossRoomPkModel->getMissionGift();
        $missList = array_column($missList,null,'mission_type');
        //完成任务
        $insertMission = [
            'user_id' => $userId,
            'mission_type' => 2,//任务类型 1=PK送礼 2=上麦任务 3=发言 4=PK胜利
            'goods_id' => $missList[2]['goods_id'],
            'send_status'=> 0,
            'created_at' => Carbon::now()->toDateTimeString(),
        ];
        $status = $this->crossRoomPkModel->insertMission($insertMission);
        if(!$status){
            write_log($this->log,'INFO 当天已完成',$insertMission);
        }
        Redis::set($this->crossRoomPkMissionComplate.$userId,1);
        $now = Carbon::now()->toDateTimeString();
        $endDay = Carbon::now()->endOfDay()->toDateTimeString();
        $diffTime = Carbon::parse($now)->diffInSeconds($endDay);
        Redis::EXPIRE($this->crossRoomPkMissionComplate.$userId,$diffTime);//当前时间到今日截止时间
    }
}